package net.bsayiner.sadeceHaber.Activities;

import java.util.ArrayList;

import net.bsayiner.sadeceHaber.R;
import net.bsayiner.sadeceHaber.Utilities.News;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NewsListAdapter extends ArrayAdapter<News> {

	private final ArrayList<News> newsList;
	private LayoutInflater inflater;

	public NewsListAdapter(Context context, int resource, ArrayList<News> newsList) {
		super(context, resource);
		this.newsList = newsList;
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		BitmapManager.INSTANCE.setPlaceholder(BitmapFactory.decodeResource(context.getResources(), R.drawable.icon));
	}

	@Override
	public int getCount() {
		return newsList.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.news_row_layout, null, false);
			TextView textView = (TextView) convertView.findViewById(R.id.label);
			ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
			viewHolder = new ViewHolder();
			viewHolder.nameTextView = textView;
			viewHolder.iconView = imageView;
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		News news = newsList.get(position);
		viewHolder.nameTextView.setText(news.getTitle());
		viewHolder.iconView.setTag(news.getImg());
		BitmapManager.INSTANCE.loadBitmap(news.getImg(), viewHolder.iconView);
		return convertView;
	}

	private static class ViewHolder {
		public ImageView iconView;
		public TextView nameTextView;
	}

}